﻿using Microsoft.Research.DynamicDataDisplay;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace NB_Controller_Test
{
    //Mac和ConnID的配对
    public class MacAndConnID
    {
        public string strMac = "";
        public IntPtr connID = IntPtr.Zero;

        public MacAndConnID(string strMacIn, IntPtr connIDIn)
        {
            strMac = strMacIn;
            connID  = connIDIn;
        }
    }

    public class MacAndConnIDManager
    {
        //Mac和ConnID的匹配列表
        private List<MacAndConnID> macAndConnIDList = new List<MacAndConnID>();

        public void UpdateItem(MacAndConnID macAndID)
        {
            foreach(MacAndConnID macID in macAndConnIDList)
            {
                if(macID.strMac == macAndID.strMac)
                {
                    macID.connID = macAndID.connID;
                    break;
                }
            }

            AddNew(macAndID);
        }

        public void AddNew(MacAndConnID macAndID)
        {
            macAndConnIDList.Add(macAndID);
        }

        public IntPtr GetIDByMac(string strMac)
        {
            foreach (MacAndConnID macID in macAndConnIDList)
            {
                if (macID.strMac == strMac)
                {
                    return macID.connID;
                }
            }

            return IntPtr.Zero;
        }

        public void Clear()
        {
            macAndConnIDList.Clear();
        }
    }

    public partial class MainUIEx : UserControl
    {
        private SendCmds sendCmds = new SendCmds();
        private DevAliasNames devAliasNames = new DevAliasNames();
        private SystemConfig systemConfig = new SystemConfig();
        private NB_ContentTypeManager typeManager = new NB_ContentTypeManager();

        MacAndConnIDManager macAndIDManager = new MacAndConnIDManager();

        public void ShowLineGraphByIp(string strIP, bool bShow = true)
        {
            bool bNewCreate = false;
            //SignalData data = sdMng.GetSignalData(strIP, out bNewCreate);
            //if (data != null)
            //{
            //    LineGraph lg = data.lineGp;
            //    if (lg != null)
            //    {
            //        if (bShow == true)
            //        {
            //            lg.Visibility = System.Windows.Visibility.Visible;
            //        }
            //        else
            //        {
            //            lg.Visibility = System.Windows.Visibility.Hidden;
            //        }
            //    }
            //}

            SignalData data = sdMng.GetSignalData(strIP + "_rsrp", false, out bNewCreate);
            if (data != null)
            {
                LineGraph lg = data.lineGp;
                if (lg != null)
                {
                    if (bShow == true)
                    {
                        lg.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        lg.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
            }

            data = sdMng.GetSignalData(strIP + "_txPower", false, out bNewCreate);
            if (data != null)
            {
                LineGraph lg = data.lineGp;
                if (lg != null)
                {
                    if (bShow == true)
                    {
                        lg.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        lg.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
            }

            data = sdMng.GetSignalData(strIP + "_snr", false, out bNewCreate);
            if (data != null)
            {
                LineGraph lg = data.lineGp;
                if (lg != null)
                {
                    if (bShow == true)
                    {
                        lg.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        lg.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
            }

            data = sdMng.GetSignalData(strIP + "_cellID", false, out bNewCreate);
            if (data != null)
            {
                LineGraph lg = data.lineGp;
                if (lg != null)
                {
                    if (bShow == true)
                    {
                        lg.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        lg.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
            }

            data = sdMng.GetSignalData(strIP + "_rssi", false, out bNewCreate);
            if (data != null)
            {
                LineGraph lg = data.lineGp;
                if (lg != null)
                {
                    if (bShow == true)
                    {
                        lg.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        lg.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
            }
        }

        public void UpdateAliasName(string strMacInfo, string strAliasName)
        {
            devAliasNames.UpdateAliasName(strMacInfo, strAliasName);
        }

        private void InitWpfChart()
        {
            lvDevList.Children.Clear();
            lvDevList1.Children.Clear();
            lvDevList2.Children.Clear();
            lvDevList3.Children.Clear();
            lvDevList4.Children.Clear();

            //去掉图例
            signlRecord_rsrp.Legend.Visibility = System.Windows.Visibility.Collapsed;// = false;
            signlRecord_txpower.Legend.Visibility = System.Windows.Visibility.Collapsed;// = false;
            signlRecord_snr.Legend.Visibility = System.Windows.Visibility.Collapsed;// = false;
            signlRecord_cellID.Legend.Visibility = System.Windows.Visibility.Collapsed;// = false;
            signlRecord_rssi.Legend.Visibility = System.Windows.Visibility.Collapsed;// = false;

            //取消表内鼠标和键盘的缩放功能
            //signlRecord_rsrp.Children.Remove(signlRecord_rsrp.MouseNavigation);
            //signlRecord_rsrp.Children.Remove(signlRecord_rsrp.KeyboardNavigation);
            //取消表内网格
            //signlRecord_rsrp.AxisGrid.Remove();
            //取消y坐标的功能
            //signlRecord_rsrp.VerticalAxisNavigation.Remove();

            //初始化系统颜色库
            #region
            colorList.Add(Colors.YellowGreen);
            //colorList.Add(Colors.Yellow);
            colorList.Add(Colors.Violet);
            colorList.Add(Colors.Turquoise);
            colorList.Add(Colors.Tomato);
            colorList.Add(Colors.Teal);
            colorList.Add(Colors.SteelBlue);
            colorList.Add(Colors.SpringGreen);
            colorList.Add(Colors.SlateBlue);
            colorList.Add(Colors.SeaGreen);

            colorList.Add(Colors.SandyBrown);
            colorList.Add(Colors.Salmon);
            colorList.Add(Colors.SaddleBrown);
            colorList.Add(Colors.RoyalBlue);
            colorList.Add(Colors.Red);
            colorList.Add(Colors.Purple);
            colorList.Add(Colors.Plum);
            colorList.Add(Colors.Peru);
            colorList.Add(Colors.MediumVioletRed);
            colorList.Add(Colors.MediumTurquoise);
            colorList.Add(Colors.MediumSpringGreen);
            colorList.Add(Colors.MediumSlateBlue);
            colorList.Add(Colors.MediumSeaGreen);
            colorList.Add(Colors.MediumPurple);
            colorList.Add(Colors.MediumOrchid);

            colorList.Add(Colors.Aqua);
            colorList.Add(Colors.Black);
            colorList.Add(Colors.Blue);
            colorList.Add(Colors.BlueViolet);
            colorList.Add(Colors.Brown);
            colorList.Add(Colors.BurlyWood);
            colorList.Add(Colors.CadetBlue);
            colorList.Add(Colors.Chartreuse);
            colorList.Add(Colors.Chocolate);
            colorList.Add(Colors.CornflowerBlue);
            colorList.Add(Colors.Crimson);
            colorList.Add(Colors.Cyan);
            colorList.Add(Colors.DarkCyan);
            colorList.Add(Colors.DarkGreen);
            colorList.Add(Colors.DarkKhaki);
            colorList.Add(Colors.DarkMagenta);
            colorList.Add(Colors.DarkOliveGreen);
            colorList.Add(Colors.DarkOrange);
            colorList.Add(Colors.DarkOrchid);
            colorList.Add(Colors.DarkRed);
            colorList.Add(Colors.DarkSalmon);
            colorList.Add(Colors.DarkSeaGreen);
            colorList.Add(Colors.DarkSlateBlue);
            colorList.Add(Colors.DarkSlateGray);
            colorList.Add(Colors.DarkTurquoise);
            colorList.Add(Colors.DarkViolet);
            colorList.Add(Colors.DeepPink);
            colorList.Add(Colors.DeepSkyBlue);
            colorList.Add(Colors.DimGray);

            colorList.Add(Colors.LightBlue);
            colorList.Add(Colors.LightCoral);
            colorList.Add(Colors.LightCyan);
            colorList.Add(Colors.LightGoldenrodYellow);
            colorList.Add(Colors.LightGray);
            colorList.Add(Colors.LightGreen);
            colorList.Add(Colors.LightPink);
            colorList.Add(Colors.LightSalmon);
            colorList.Add(Colors.LightSeaGreen);
            colorList.Add(Colors.LightSkyBlue);
            colorList.Add(Colors.LightSlateGray);
            colorList.Add(Colors.LightSteelBlue);
            colorList.Add(Colors.LightYellow);
            #endregion

            //Test Add
            //int nIndex = 1;
            //foreach (Color col in colorList)
            //{
            //    DevItemView cb = new DevItemView();
            //    cb.check.Background = new SolidColorBrush(col);
            //    cb.index.Text = (nIndex++).ToString();
            //    //lvLines.Items.Add(cb);
            //}
        }

        private void InitCmdBut()
        {
            dataTable_QuickCmd.Clear();
            cmdCanvas.Children.Clear();
            thumbList.Clear();

            //根据配置文件填充信号周期信息
            string strValue = systemConfig.GetValueByName("SignalReTime");
            tbSignalReTime.Text = strValue;

            cmdCanvas.Children.Clear();

            int nSize = sendCmds.sendCmdArry.Count; ;//.uiSendCmdCount;

            cmdCanvas.Width = svCmd.ActualWidth + 10;
            cmdCanvas.Height = svCmd.ActualHeight + 50;

            int mParentWidth = (int)cmdCanvas.Width;
            int nThumbWidth = 110;
            int nThumbHeight = 30;
            int count = mParentWidth / (nThumbWidth + 5);
            int nTop = 0;
            int nLeft = 0;
            int nRow = 0;
            int nColumn = 0;

            ButThumb butAdd = null;
            for (int i = 0; i < nSize; i++)
            {
                nColumn = i % count;
                nLeft = nColumn * nThumbWidth;
                nRow = i / count;
                nTop = nRow * nThumbHeight;

                SendCmds.SendCmd sendCmd = sendCmds.sendCmdArry[i];

                //cmdWrapPanel
                butAdd = new ButThumb();
                //butAdd.Margin = new Thickness(2);
                butAdd.strTitle = sendCmd.strName;
                butAdd.Index = zIndex++;
                butAdd.ParentControl = cmdCanvas;
                //拖动改变完位置后，这个变量需要更新？？？
                butAdd.RectangleGeometry = new RectangleGeometry() { Rect = new Rect(5 + nLeft + 3 * nColumn, nTop - 5 * nRow, nThumbWidth, nThumbHeight) };
                //butAdd.Width = wth;
                butAdd.Height = nThumbHeight;
                butAdd.ToolTip = sendCmd.strName;
                
                //string col = "#FFD8ECD1";
                //this.BackColor = System.Drawing.ColorTranslator.FromHtml(col); 
                butAdd.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xD8, 0xEC, 0xD1));
                //butAdd.Background = new SolidColorBrush(Colors.LightBlue);
                //LightBlue
                //butAdd.MouseDownClick += butQuickCmd_Click;
                butAdd.MouseRightButtonUp += butQuickCmd_RightClick;

                Canvas.SetLeft(butAdd, 5 + nLeft + 3 * nColumn);
                Canvas.SetTop(butAdd, nTop - 5 * nRow);
                Canvas.SetZIndex(butAdd, butAdd.Index);
                butAdd.DragDelta += mdThumb_DragDelta;
                butAdd.MyMouseLeftButtonDown += mdThumb_MyMouseLeftButtonDown;
                butAdd.MyMouseLeftButtonUp += mdThumb_MyMouseLeftButtonUp;
                butAdd.MouseDownClick += butQuickCmd_Click;

                cmdCanvas.Children.Add(butAdd);
                thumbList.Add(butAdd);
            }

            if(butAdd != null)
            {
                cmdCanvas.Width = svCmd.ActualWidth + 10;
                cmdCanvas.Height = Canvas.GetTop(butAdd) + nThumbHeight;
            }

            //cmdCanvas.UpdateLayout();
            //svCmd.ScrollToEnd();
            //svCmd.UpdateLayout();

            //快捷命令发送接收队列记录表列标题
            dataTable_QuickCmd.Columns.Add(new DataColumn("序号", typeof(string)));
            dataTable_QuickCmd.Columns.Add(new DataColumn("mac", typeof(string)));
            dataTable_QuickCmd.Columns.Add(new DataColumn("imei", typeof(string)));
            dataTable_QuickCmd.Columns.Add(new DataColumn("发送时间", typeof(string)));
            dataTable_QuickCmd.Columns.Add(new DataColumn("SendData", typeof(string)));
            dataTable_QuickCmd.Columns.Add(new DataColumn("接收时间", typeof(string)));
            dataTable_QuickCmd.Columns.Add(new DataColumn("RecvData", typeof(string)));

            return;

            #region
            //int w = 400;// (int)cmdWrapPanel.ActualWidth;
            //int wth = 30;
            //int count = w / (wth + 10);
            //int top = 0;
            //int left = 0;
            //int row = 0;
            //int column = 0;
            //for (int i = 0; i < 50; i++)
            //{
            //    column = i % count;
            //    left = column * 40;
            //    row = i / count;
            //    top = row * 40;

            //    ButThumb mouseDownThumb = new ButThumb()
            //    {
            //        Height = wth,
            //        Width = wth,
            //        Background = new SolidColorBrush(Colors.Red),

            //        //Template = ct,
            //        Index = i,
            //        RectangleGeometry = new RectangleGeometry() { Rect = new Rect(left, top, wth, wth) },
            //        ParentControl = cmdWrapPanel,
            //        strTitle = i.ToString()
            //    };

            //    //mouseDownThumb.Template.Resources.
            //    //mouseDownThumb.ParentControl = can;
            //    //mouseDownThumb.DataContext = "1123";
            //    mouseDownThumb.DragDelta += mdThumb_DragDelta;
            //    mouseDownThumb.MyMouseLeftButtonDown += mdThumb_MyMouseLeftButtonDown;
            //    mouseDownThumb.MyMouseLeftButtonUp += mdThumb_MyMouseLeftButtonUp;
            //    //mouseDownThumb.MouseDownClick += mouseDownThumb_MouseDownClick;
            //    Canvas.SetLeft(mouseDownThumb, left);
            //    Canvas.SetTop(mouseDownThumb, top);
            //    Canvas.SetZIndex(mouseDownThumb, zIndex);
            //    cmdWrapPanel.Children.Add(mouseDownThumb);
            //    zIndex++;
            //    thumbList.Add(mouseDownThumb);
            //}
            #endregion
        }

        #region Thumb
        private int zIndex = 0;
        List<ButThumb> thumbList = new List<ButThumb>();
        //private ButThumb lastThumb = null;
        private void mdThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            ButThumb mdThumb = sender as ButThumb;
            mdThumb.bIsDraged = true;

            double toolLeft = Canvas.GetLeft(mdThumb) + e.HorizontalChange;
            double toolTop = Canvas.GetTop(mdThumb) + e.VerticalChange;

            toolLeft = toolLeft < 0 ? 0 : toolLeft;
            toolTop = toolTop < 0 ? 0 : toolTop;

            toolLeft = this.Width < (mdThumb.Width + toolLeft) ? (this.Width - mdThumb.Width) : toolLeft;
            toolTop = this.Height < (mdThumb.Height + toolTop) ? (this.Height - mdThumb.Height) : toolTop;

            Canvas.SetLeft(mdThumb, toolLeft);
            Canvas.SetTop(mdThumb, toolTop);
            //t.Text = toolLeft + "," + toolTop;
            //t1.Text = Canvas.GetLeft(mdThumb) + "," + Canvas.GetTop(mdThumb);
            Point point = Mouse.GetPosition(cmdCanvas);//can
            ButThumb coverThumb = thumbList.Find(p => p.RectangleGeometry.FillContains(point));
            if (coverThumb != null)
            {
                RectangleGeometry tempRect = coverThumb.RectangleGeometry;                
                coverThumb.RectangleGeometry = mdThumb.RectangleGeometry;
                mdThumb.RectangleGeometry = tempRect;

                //Index互换
                int nIndex = coverThumb.Index;
                coverThumb.Index = mdThumb.Index;
                mdThumb.Index = nIndex;                

                //lastThumb = coverThumb;
                Back(coverThumb);
            }
        }

        private void mdThumb_MyMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ButThumb mdThumb = sender as ButThumb;
            System.Diagnostics.Debug.WriteLine("Index: " + mdThumb.Index.ToString());
            //mdThumb.bIsDraged = false;
            mdThumb.CaptureMouse();
            Canvas.SetZIndex(mdThumb, zIndex++);
            thumbList.Remove(mdThumb);
        }

        private void mdThumb_MyMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ButThumb mdThumb = sender as ButThumb;
            //mdThumb.bIsDraged = false;
            mdThumb.ReleaseMouseCapture();
            if (thumbList.Find(p => p.Index == mdThumb.Index) == null)
            {
                thumbList.Add(mdThumb);

                //if(lastThumb != null)
                //{
                //    Button butCmd = (Button)mdThumb.Template.FindName("Button", mdThumb);
                //    Button butCoverCmd = (Button)lastThumb.Template.FindName("Button", lastThumb);
                //    sendCmds.SortSendCmdsByNames(butCoverCmd.Content.ToString(), butCmd.Content.ToString());
                //}

                //lastThumb = null;
            }

            Back(mdThumb);
        }

        private void Back(ButThumb mdThumb)
        {
            double left = Canvas.GetLeft(mdThumb);
            double top = Canvas.GetTop(mdThumb);

            Storyboard storyboard = new Storyboard();
            DoubleAnimation animation = CreateAnimation(left, mdThumb.RectangleGeometry.Rect.Left, mdThumb, "(Canvas.Left)");
            storyboard.Children.Add(animation);
            animation = CreateAnimation(top, mdThumb.RectangleGeometry.Rect.Top, mdThumb, "(Canvas.Top)");
            storyboard.Children.Add(animation);
            storyboard.Completed += (sender1, e1) =>
            {
                storyboard.Stop();
                storyboard = null;
                Canvas.SetLeft(mdThumb, mdThumb.RectangleGeometry.Rect.Left);
                Canvas.SetTop(mdThumb, mdThumb.RectangleGeometry.Rect.Top);
            };
            storyboard.Begin();

        }

        private DoubleAnimation CreateAnimation(double? from, double? to, DependencyObject value, string property)
        {
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = from;
            animation.To = to;
            animation.Duration = TimeSpan.FromMilliseconds(100);
            // animation.FillBehavior = FillBehavior.Stop;
            Storyboard.SetTarget(animation, value);
            Storyboard.SetTargetProperty(animation, new PropertyPath(property));
            return animation;
        }
        #endregion

        private ObservableCollection<LogDataItem> dataPackInfoList = new ObservableCollection<LogDataItem>();
        ObservableCollection<LogDataItem> DataPackInfoList
        {
            get { return dataPackInfoList; }
            set { dataPackInfoList = value; }
        }
        private void InitLogList()
        {
            //数据重设定
            listViewLog.ItemsSource = DataPackInfoList;
            return;

            ////数据源集合为ObservableCollection<T>类型，保证数据源和ListView试图同步。                            
            //List<LogDataItem> listTemp = new List<LogDataItem>();
            //ObservableCollection<LogDataItem> dataItems = listView.ItemsSource as ObservableCollection<LogDataItem>;
            //foreach (LogDataItem item in /*DataPackInfoList*/dataItems)
            //{
            //    listTemp.Add(item);
            //}

            //listTemp.Reverse();

            ////数据重设定
            //dataItems.Clear();
            //foreach (LogDataItem item in listTemp)
            //{
            //    dataItems.Add(item);
            //}

            //listView.ItemsSource = dataItems;
        }

        public static DataTable dataTableDev = new DataTable("NB_DevList");
        private void InitDataTable()
        {
            string strLineTag = Convert.ToChar(10).ToString();
            //dataTableDev.Columns.Add(new DataColumn("序号&#xD;&#xA;（必填）", typeof(string)));
            dataTableDev.Columns.Add(new DataColumn("序号" + strLineTag + "（必填）", typeof(string)));
            dataTableDev.Columns.Add(new DataColumn("UID" + strLineTag + "（必填）", typeof(string)));//IMEI
            dataTableDev.Columns.Add(new DataColumn("名称" + strLineTag + "（必填）", typeof(string)));//别名
            dataTableDev.Columns.Add(new DataColumn("经度", typeof(string)));
            dataTableDev.Columns.Add(new DataColumn("纬度", typeof(string)));
            dataTableDev.Columns.Add(new DataColumn("DeviceID" + strLineTag + "（非必填项）", typeof(string)));
            dataTableDev.Columns.Add(new DataColumn("Relevance Lamp" + strLineTag + "（uid）" + strLineTag + "（非必填项）", typeof(string)));
            dataTableDev.Columns.Add(new DataColumn("备注", typeof(string)));
            dataTableDev.Columns.Add(new DataColumn("版本", typeof(string)));
        }

        private void UninitDataTable()
        {
            if (dataTableDev != null)
            {
                //GlobalFun.WriteExcel(dataTableDev, System.Windows.Forms.Application.StartupPath + "\\DevList.xls");
                dataTableDev.Clear();
            }
        }

        private void UpdateGprsSetInfo()
        {
            if (alias2GprsList == null)
                return;

            //alias2GprsList
            foreach (DevItemView devItem in lvDevList.Children)
            {
                string strAliasName = devItem.aliasName.Text;

                foreach (CreateGPRSInfo.Alias2Gprs ag in alias2GprsList)
                {
                    if (ag.strAliasName == strAliasName)
                    {
                        //ag.ptGprs
                        devItem.tb_Set_JingDu.Text = ag.ptGprs.X.ToString();
                        devItem.tb_Set_WeiDu.Text = ag.ptGprs.Y.ToString();

                        devItem.SetGprsState();
                        break;
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList1.Children)
            {
                string strAliasName = devItem.aliasName.Text;

                foreach (CreateGPRSInfo.Alias2Gprs ag in alias2GprsList)
                {
                    if (ag.strAliasName == strAliasName)
                    {
                        //ag.ptGprs
                        devItem.tb_Set_JingDu.Text = ag.ptGprs.X.ToString();
                        devItem.tb_Set_WeiDu.Text = ag.ptGprs.Y.ToString();

                        devItem.SetGprsState();
                        break;
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList2.Children)
            {
                string strAliasName = devItem.aliasName.Text;

                foreach (CreateGPRSInfo.Alias2Gprs ag in alias2GprsList)
                {
                    if (ag.strAliasName == strAliasName)
                    {
                        //ag.ptGprs
                        devItem.tb_Set_JingDu.Text = ag.ptGprs.X.ToString();
                        devItem.tb_Set_WeiDu.Text = ag.ptGprs.Y.ToString();

                        devItem.SetGprsState();
                        break;
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList3.Children)
            {
                string strAliasName = devItem.aliasName.Text;

                foreach (CreateGPRSInfo.Alias2Gprs ag in alias2GprsList)
                {
                    if (ag.strAliasName == strAliasName)
                    {
                        //ag.ptGprs
                        devItem.tb_Set_JingDu.Text = ag.ptGprs.X.ToString();
                        devItem.tb_Set_WeiDu.Text = ag.ptGprs.Y.ToString();

                        devItem.SetGprsState();
                        break;
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList4.Children)
            {
                string strAliasName = devItem.aliasName.Text;

                foreach (CreateGPRSInfo.Alias2Gprs ag in alias2GprsList)
                {
                    if (ag.strAliasName == strAliasName)
                    {
                        //ag.ptGprs
                        devItem.tb_Set_JingDu.Text = ag.ptGprs.X.ToString();
                        devItem.tb_Set_WeiDu.Text = ag.ptGprs.Y.ToString();

                        devItem.SetGprsState();
                        break;
                    }
                }
            }
        }

        private void UpdateDevItemVisible()
        {
            bool bShowCurVer = cbCheckVer.IsChecked == true;

            foreach (DevItemView devItem in lvDevList.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ver.Text == tbOTABinFileVer.Text)
                    {
                        if (bShowCurVer)
                        {
                            //显示
                            devItem.Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            //隐藏
                            devItem.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList1.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ver.Text == tbOTABinFileVer.Text)
                    {
                        if (bShowCurVer)
                        {
                            //显示
                            devItem.Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            //隐藏
                            devItem.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList2.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ver.Text == tbOTABinFileVer.Text)
                    {
                        if (bShowCurVer)
                        {
                            //显示
                            devItem.Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            //隐藏
                            devItem.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList3.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ver.Text == tbOTABinFileVer.Text)
                    {
                        if (bShowCurVer)
                        {
                            //显示
                            devItem.Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            //隐藏
                            devItem.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList4.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ver.Text == tbOTABinFileVer.Text)
                    {
                        if (bShowCurVer)
                        {
                            //显示
                            devItem.Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            //隐藏
                            devItem.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                }
            }
        }

        private DevItemView FindDevItemViewByIP(string strIP)
        {
            foreach (DevItemView devItem in lvDevList.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ip.Text.ToString() == strIP)
                    {
                        return devItem;
                    }
                }
            }
            foreach (DevItemView devItem in lvDevList1.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ip.Text.ToString() == strIP)
                    {
                        return devItem;
                    }
                }
            }
            foreach (DevItemView devItem in lvDevList2.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ip.Text.ToString() == strIP)
                    {
                        return devItem;
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList3.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ip.Text.ToString() == strIP)
                    {
                        return devItem;
                    }
                }
            }

            foreach (DevItemView devItem in lvDevList4.Children)
            {
                if (devItem != null)
                {
                    if (devItem.ip.Text.ToString() == strIP)
                    {
                        return devItem;
                    }
                }
            }

            //白色认为特殊
            return null;
        }

        private Color FindColorByIp(string strIP)
        {
            DevItemView devItem = FindDevItemViewByIP(strIP);
            if (devItem != null)
            {
                return (Color)ColorConverter.ConvertFromString(devItem.check.Background.ToString());
            }

            //黑色认为特殊
            return Colors.Black;
        }
        
        private bool AddDevInfo(IntPtr connID, JToken jtWifiInfo, JToken jtNBBaseInfo, JToken jtFWV, JToken jtLoc)
        {
            string strMacInfo = "???";
            string strVerInfo = "???";

            #region 提取出有用信息
            //string strProp = jtProp.ToString();            

            //WifiInfo
            #region
            //JToken jtWifiInfo = jtProp.SelectToken("wifiInfo");
            if (jtWifiInfo != null)
            {
                JToken jtMac = jtWifiInfo.SelectToken("mac");
                if (jtMac != null)
                {
                    strMacInfo = jtMac.ToString();
                    //AddDevInfo(connID, jtProp);
                }
            }
            else
            {
                return false; ;
            }
            #endregion

            //fwv
            #region
            //JToken jtFWV = jtProp.SelectToken("fwv");
            if (jtFWV != null)
            {
                strVerInfo = jtFWV.ToString();

                //DevItemView devItem = FindDevItemViewByIP(strIP);
                //if (devItem != null)
                //{
                //    devItem.ver.Text = strVer;
                //    //判断是否在OTA
                //    if (devItem.pbOTAing.Visibility == System.Windows.Visibility.Visible)
                //    {
                //        if (strVer == tbOTAVer.Text)
                //        {
                //            //OTA成功
                //            devItem.SetOTAState(DevItemView.byOTASuccess);
                //        }
                //    }
                //}
            }
            #endregion

            #endregion

            //IP Port解析
            string strIP = "";
            UInt16 uiPort = 0;
            NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

            //IP下拉框，使用的是ConnID 为Tag
            //先确认要添加的ConnID是否已添加过
            bool bAddToCombo = true;
            foreach (ComboBoxItem item in devIP.Items)
            {
                if ((item.Tag.ToString()) == strMacInfo)
                {
                    bAddToCombo = false;
                    //更新Mac和ConnID

                    break;
                }
            }
            if (bAddToCombo)
            {
                ComboBoxItem itemIP = new ComboBoxItem();
                itemIP.Content = strIP + "--[mac:" + strMacInfo + "]";
                itemIP.Tag = strMacInfo;
                devIP.Items.Add(itemIP);
                //保留记录
                macAndIDManager.AddNew(new MacAndConnID(strMacInfo, connID));
            }


            //设备列表
            //这里需要保护，防止出现集合被更改情况
            //遍历判断是否已存在相同Mac的设备
            foreach (DevItemView devItem in lvDevList.Children)
            {
                if(devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo;
                    devItem.online.Text = "online";
                    return false;
                }
            }
            foreach (DevItemView devItem in lvDevList1.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo;
                    devItem.online.Text = "online";
                    return false;
                }
            }
            foreach (DevItemView devItem in lvDevList2.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo;
                    devItem.online.Text = "online";
                    return false;
                }
            }

            foreach (DevItemView devItem in lvDevList3.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo;
                    devItem.online.Text = "online";
                    return false;
                }
            }

            foreach (DevItemView devItem in lvDevList4.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo;
                    devItem.online.Text = "online";
                    return false;
                }
            }

            string strImeiInfo = "???";            
            string strJLocInfo = "???";
            string strWLocInfo = "???";

            #region 提取出有用信息
            //string strProp = jtProp.ToString();            

            //WifiInfo
            #region
            //JToken jtWifiInfo = jtProp.SelectToken("wifiInfo");
            //if (jtWifiInfo != null)
            //{
            //    JToken jtMac = jtWifiInfo.SelectToken("mac");
            //    if (jtMac != null)
            //    {
            //        strMacInfo = jtMac.ToString();
            //        //AddDevInfo(connID, jtProp);
            //    }
            //}
            //else
            //{
            //    return false;
            //}
            #endregion

            //nbBase
            #region
            //JToken jtBNBaseInfo = jtProp.SelectToken("nbBase");
            if (jtNBBaseInfo != null)
            {
                JToken jtImei = jtNBBaseInfo.SelectToken("imei");
                if (jtImei != null)
                {
                    strImeiInfo = jtImei.ToString();
                }
            }
            #endregion

            

            //loc
            #region
            //JToken jtLoc = jtProp.SelectToken("loc");
            if (jtLoc != null)
            {
                JToken jtLat = jtLoc.SelectToken("lat");
                if (jtLat != null)
                {
                    strJLocInfo = jtLat.ToString();
                    //AddDevInfo(connID, jtProp);
                }
                JToken jtLng = jtLoc.SelectToken("lng");
                if (jtLng != null)
                {
                    strWLocInfo = jtLng.ToString();
                    //AddDevInfo(connID, jtProp);
                }
            }
            #endregion
            #endregion

            //增加新设备
            DevItemView devItemNew = new DevItemView();
            int nCount = lvDevList.Children.Count;
            if (nCount >= colorList.Count)
            {
                //设备数量超过颜色数量，则设备数量取余数后使用
                int nIndex = nCount % colorList.Count;
                devItemNew.check.Background = new SolidColorBrush(colorList.ElementAt(nIndex));
            }
            else
            {
                devItemNew.check.Background = new SolidColorBrush(colorList.ElementAt(nCount));
            }
            devItemNew.index.Text = (nCount + 1).ToString();
            devItemNew.Tag = connID;
            devItemNew.ip.Text = strIP;// +strIMEIInfo;
            devItemNew.mac.Text = strMacInfo;//.Replace("--", "");
            devItemNew.imei.Text = strImeiInfo;
            devItemNew.ver.Text = strVerInfo;// +" [" + strJLocInfo + "," + strWLocInfo + "]";
            devItemNew.tb_Read_JingDu.Text = strJLocInfo;
            devItemNew.tb_Read_WeiDu.Text = strWLocInfo;
            //devItemNew.SetGprsState();
            devItemNew.connID.Tag = connID;
            devItemNew.checkOTA.IsChecked = cbCheckAll.IsChecked;

            //根据Mac，反查名称，若没有名称则使用Mac作为默认名称
            string strAliasName = devAliasNames.FindDevAliasNameByMac(strMacInfo).strName;
            devItemNew.aliasName.Text = strAliasName;

            devItemNew.check.IsChecked = true;
            lvDevList.Children.Add(devItemNew);

            ////更新GPRS信息
            //UpdateGprsSetInfo();

            //判断是否需要显示GPRS相关
            //if(cbShowGPRSInfo.IsChecked == false)
            //{
            //    devItemNew.spGPRSInfo.Visibility = System.Windows.Visibility.Collapsed;
            //}

            //设备列表
            //dataTableDev.Rows.Add(devItemNew.index.Text, devItemNew.imei.Text, devItemNew.aliasName.Text, strJLocInfo, strWLocInfo, "00-00-00-00-00-00 " + devItemNew.mac.Text, "", "");

            return true;
        }

        private bool AddDevInfo_Ex(IntPtr connID, JToken jtWifiInfo, JToken jtNBBaseInfo, JToken jtFWV, JToken jtLoc, JToken jtClas, JToken jtSku, JToken jtASku)
        {
            string strMacInfo = "???";
            string strVerInfo = "???";
            string strClasInfo = "???";
            string strSkuInfo = "???";
            string strASkuInfo = "???";

            #region 提取出有用信息
            //string strProp = jtProp.ToString();            

            //WifiInfo
            #region
            //JToken jtWifiInfo = jtProp.SelectToken("wifiInfo");
            if (jtWifiInfo != null)
            {
                JToken jtMac = jtWifiInfo.SelectToken("mac");
                if (jtMac != null)
                {
                    strMacInfo = jtMac.ToString();
                    //AddDevInfo(connID, jtProp);
                }
            }
            else
            {
                return false; ;
            }
            #endregion

            //fwv
            #region
            //JToken jtFWV = jtProp.SelectToken("fwv");
            if (jtFWV != null)
            {
                strVerInfo = jtFWV.ToString();
            }
            #endregion

            //clas sku asku
            #region
            if (jtClas!= null)
            {
                strClasInfo = jtClas.ToString();
            }
            if (jtSku != null)
            {
                strSkuInfo = jtSku.ToString();
            }
            if (jtASku != null)
            {
                strASkuInfo = jtASku.ToString();
            }
            #endregion

            #endregion

            //IP Port解析
            string strIP = "";
            UInt16 uiPort = 0;
            NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

            //IP下拉框，使用的是ConnID 为Tag
            //先确认要添加的ConnID是否已添加过
            bool bAddToCombo = true;
            foreach (ComboBoxItem item in devIP.Items)
            {
                if ((item.Tag.ToString()) == strMacInfo)
                {
                    bAddToCombo = false;
                    //更新Mac和ConnID

                    break;
                }
            }
            if (bAddToCombo)
            {
                ComboBoxItem itemIP = new ComboBoxItem();
                itemIP.Content = strIP + "--[mac:" + strMacInfo + "]";
                itemIP.Tag = strMacInfo;
                devIP.Items.Add(itemIP);
                //保留记录
                macAndIDManager.AddNew(new MacAndConnID(strMacInfo, connID));
            }


            //设备列表
            //这里需要保护，防止出现集合被更改情况
            //遍历判断是否已存在相同Mac的设备
            foreach (DevItemView devItem in lvDevList.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(" + strASkuInfo + ")";
                    devItem.online.Text = "online";
                    return false;
                }
            }
            foreach (DevItemView devItem in lvDevList1.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(" + strASkuInfo + ")";
                    devItem.online.Text = "online";
                    return false;
                }
            }
            foreach (DevItemView devItem in lvDevList2.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(" + strASkuInfo + ")";
                    devItem.online.Text = "online";
                    return false;
                }
            }

            foreach (DevItemView devItem in lvDevList3.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(" + strASkuInfo + ")";
                    devItem.online.Text = "online";
                    return false;
                }
            }

            foreach (DevItemView devItem in lvDevList4.Children)
            {
                if (devItem.mac.Text == strMacInfo)
                {
                    //更新ConnID
                    devItem.Tag = connID;
                    devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(" + strASkuInfo + ")";
                    devItem.online.Text = "online";
                    return false;
                }
            }

            string strImeiInfo = "???";
            string strJLocInfo = "???";
            string strWLocInfo = "???";

            #region 提取出有用信息
            //string strProp = jtProp.ToString();            

            //WifiInfo
            #region
            //JToken jtWifiInfo = jtProp.SelectToken("wifiInfo");
            //if (jtWifiInfo != null)
            //{
            //    JToken jtMac = jtWifiInfo.SelectToken("mac");
            //    if (jtMac != null)
            //    {
            //        strMacInfo = jtMac.ToString();
            //        //AddDevInfo(connID, jtProp);
            //    }
            //}
            //else
            //{
            //    return false;
            //}
            #endregion

            //nbBase
            #region
            //JToken jtBNBaseInfo = jtProp.SelectToken("nbBase");
            if (jtNBBaseInfo != null)
            {
                JToken jtImei = jtNBBaseInfo.SelectToken("imei");
                if (jtImei != null)
                {
                    strImeiInfo = jtImei.ToString();
                }
            }
            #endregion

            //loc
            #region
            //JToken jtLoc = jtProp.SelectToken("loc");
            if (jtLoc != null)
            {
                JToken jtLat = jtLoc.SelectToken("lat");
                if (jtLat != null)
                {
                    strJLocInfo = jtLat.ToString();
                    //AddDevInfo(connID, jtProp);
                }
                JToken jtLng = jtLoc.SelectToken("lng");
                if (jtLng != null)
                {
                    strWLocInfo = jtLng.ToString();
                    //AddDevInfo(connID, jtProp);
                }
            }
            #endregion
            #endregion

            //增加新设备
            DevItemView devItemNew = new DevItemView();

            WrapPanel wpCurList = null;
            int nCount = 0;
            if(lvDevList.Tag == null)
            {
                nCount = lvDevList.Children.Count;
                lvDevList.Tag = strClasInfo + "|" + strSkuInfo;
                wpCurList = lvDevList;
            }
            else
            {
                //判断是否与期望的clas一致，若不一致，则设定lvDevList1
                if ( (strClasInfo + "|" + strSkuInfo) == lvDevList.Tag.ToString())
                {
                    nCount = lvDevList.Children.Count;
                    wpCurList = lvDevList;
                }
                else
                {
                    if (lvDevList1.Tag == null)
                    {
                        nCount = lvDevList1.Children.Count;
                        lvDevList1.Tag = strClasInfo + "|" + strSkuInfo;
                        wpCurList = lvDevList1;
                    }
                    else
                    {
                        //判断是否与期望的clas一致，若不一致，则设定lvDevList2
                        if ((strClasInfo + "|" + strSkuInfo) == lvDevList1.Tag.ToString())
                        {
                            nCount = lvDevList1.Children.Count;
                            wpCurList = lvDevList1;
                        }
                        else
                        {
                            if (lvDevList2.Tag == null)
                            {
                                nCount = lvDevList2.Children.Count;
                                lvDevList2.Tag = strClasInfo + "|" + strSkuInfo;
                                wpCurList = lvDevList2;
                            }
                            else
                            {
                                //判断是否与期望的clas一致，若不一致，则设定lvDevList1
                                if ((strClasInfo + "|" + strSkuInfo) == lvDevList2.Tag.ToString())
                                {
                                    nCount = lvDevList2.Children.Count;
                                }
                                else
                                {
                                    if (lvDevList3.Tag == null)
                                    {
                                        nCount = lvDevList3.Children.Count;
                                        lvDevList3.Tag = strClasInfo + "|" + strSkuInfo;
                                        wpCurList = lvDevList3;
                                    }
                                    else
                                    {
                                        //判断是否与期望的clas一致，若不一致，则设定lvDevList1
                                        if ((strClasInfo + "|" + strSkuInfo) == lvDevList3.Tag.ToString())
                                        {
                                            nCount = lvDevList3.Children.Count;
                                        }
                                        else
                                        {
                                            if (lvDevList4.Tag == null)
                                            {
                                                nCount = lvDevList4.Children.Count;
                                                lvDevList4.Tag = strClasInfo + "|" + strSkuInfo;
                                                wpCurList = lvDevList4;
                                            }
                                            else
                                            {
                                                //判断是否与期望的clas一致，若不一致，则设定lvDevList1
                                                if ((strClasInfo + "|" + strSkuInfo) == lvDevList4.Tag.ToString())
                                                {
                                                    nCount = lvDevList4.Children.Count;
                                                }
                                                else
                                                {
                                                    //没有多余的了，默认使用lvDevList2
                                                    nCount = lvDevList4.Children.Count;
                                                }
                                                wpCurList = lvDevList4;
                                            }
                                        }
                                        //wpCurList = lvDevList3;
                                    }
                                }
                                //wpCurList = lvDevList2;
                            }
                        }
                    }
                }
            }
            //int nCount = lvDevList.Children.Count;
            if (nCount >= colorList.Count)
            {
                //设备数量超过颜色数量，则设备数量取余数后使用
                int nIndex = nCount % colorList.Count;
                devItemNew.check.Background = new SolidColorBrush(colorList.ElementAt(nIndex));
            }
            else
            {
                devItemNew.check.Background = new SolidColorBrush(colorList.ElementAt(nCount));
            }
            devItemNew.index.Text = (nCount + 1).ToString();
            devItemNew.Tag = connID;
            devItemNew.ip.Text = strIP;// +strIMEIInfo;
            devItemNew.mac.Text = strMacInfo;//.Replace("--", "");
            devItemNew.imei.Text = strImeiInfo;
            devItemNew.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(" + strASkuInfo + ")";// +" [" + strJLocInfo + "," + strWLocInfo + "]";
            devItemNew.tb_Read_JingDu.Text = strJLocInfo;
            devItemNew.tb_Read_WeiDu.Text = strWLocInfo;
            //devItemNew.SetGprsState();
            devItemNew.connID.Tag = connID;
            devItemNew.checkOTA.IsChecked = cbCheckAll.IsChecked;

            //根据Mac，反查名称，若没有名称则使用Mac作为默认名称
            string strAliasName = devAliasNames.FindDevAliasNameByMac(strMacInfo).strName;
            devItemNew.aliasName.Text = strAliasName;

            devItemNew.check.IsChecked = true;
            wpCurList.Children.Add(devItemNew);

            return true;
        }

        private void DoFWVInfo(IntPtr connID, JToken jtFWV)
        {
            //单独到这里表示可能在OTA升级，判断版本是否变化，若变化表示OTA完成
            if (jtFWV != null)
            {
                //IP Port解析
                string strIP = "";
                UInt16 uiPort = 0;
                NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

                string strVer = jtFWV.ToString();

                foreach (DevItemView devItem in lvDevList.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVer;
                        break;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList1.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVer;
                        break;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList2.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVer;
                        break;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList3.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVer;
                        break;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList4.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVer;
                        break;
                    }
                    else
                    {
                    }
                }
            }
        }

        private void DoFWV_Clas_SkuInfo(IntPtr connID, JToken jtFWV, JToken jtClas, JToken jtSku)
        {
            //单独到这里表示可能在OTA升级，判断版本是否变化，若变化表示OTA完成
            if (jtFWV != null)
            {
                //IP Port解析
                string strIP = "";
                UInt16 uiPort = 0;
                NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

                string strVerInfo = jtFWV.ToString();
                string strSkuInfo = jtSku.ToString();
                string strClasInfo = jtClas.ToString();
                //目前clas f011表示Nema设备 f012表示NNema设备                

                //判断clas是否与lvDevList.Tag一致，若不一致还在里面，则更新到对应的lvDevList？里
                #region
                //if (lvDevList.Tag == null)
                //{
                //    lvDevList.Tag = strClasInfo;
                //    bool bOK = DoUpdateVerInfo(lvDevList, strIP, strVerInfo, strSkuInfo, strClasInfo);
                //    if(bOK)
                //    {
                //        MessageBox.Show("无法更新 " + strIP + " 的相关信息，请确认[1]！");
                //    }
                //}
                //else
                //{
                //    if(lvDevList.Tag.ToString() == strClasInfo)
                //    {
                //        bool bOK = DoUpdateVerInfo(lvDevList, strIP, strVerInfo, strSkuInfo, strClasInfo);
                //        if (bOK)
                //        {
                //            MessageBox.Show("无法更新 " + strIP + " 的相关信息，请确认[2]！");
                //        }
                //    }
                //    else
                //    {
                //        if (lvDevList1.Tag == null)
                //        {
                //            lvDevList1.Tag = strClasInfo;
                //            bool bOK = DoUpdateVerInfo(lvDevList1, strIP, strVerInfo, strSkuInfo, strClasInfo);
                //            if (bOK)
                //            {
                //                MessageBox.Show("无法更新 " + strIP + " 的相关信息，请确认[3]！");
                //            }
                //        }
                //        else
                //        {
                //            if (lvDevList1.Tag.ToString() == strClasInfo)
                //            {
                //                bool bOK = DoUpdateVerInfo(lvDevList1, strIP, strVerInfo, strSkuInfo, strClasInfo);
                //                if (bOK)
                //                {
                //                    MessageBox.Show("无法更新 " + strIP + " 的相关信息，请确认[4]！");
                //                }
                //            }
                //            else
                //            {
                //                if (lvDevList2.Tag == null)
                //                {
                //                    lvDevList2.Tag = strClasInfo;
                //                    bool bOK = DoUpdateVerInfo(lvDevList2, strIP, strVerInfo, strSkuInfo, strClasInfo);
                //                    if (bOK)
                //                    {
                //                        MessageBox.Show("无法更新 " + strIP + " 的相关信息，请确认[5]！");
                //                    }
                //                }
                //                else
                //                {
                //                    if (lvDevList2.Tag.ToString() == strClasInfo)
                //                    {
                //                        bool bOK = DoUpdateVerInfo(lvDevList2, strIP, strVerInfo, strSkuInfo, strClasInfo);
                //                        if (bOK)
                //                        {
                //                            MessageBox.Show("无法更新 " + strIP + " 的相关信息，请确认[6]！");
                //                        }
                //                    }
                //                    else
                //                    {
                //                        //借用lvDevList2
                //                        bool bOK = DoUpdateVerInfo(lvDevList2, strIP, strVerInfo, strSkuInfo, strClasInfo);
                //                        if (bOK)
                //                        {
                //                            MessageBox.Show("无法更新 " + strIP + " 的相关信息，请确认[7]！");
                //                        }
                //                    }
                //                }
                //            }
                //        }
                //    }
                //}
                #endregion

                #region 逐个遍历
                foreach (DevItemView devItem in lvDevList.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(???)";// +" [" + strJLocInfo + "," + strWLocInfo + "]";
                        return;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList1.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(???)";// +" [" + strJLocInfo + "," + strWLocInfo + "]";
                        return;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList2.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(???)";// +" [" + strJLocInfo + "," + strWLocInfo + "]";
                        return;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList3.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(???)";// +" [" + strJLocInfo + "," + strWLocInfo + "]";
                        return;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList4.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        devItem.ver.Text = strVerInfo + "|" + strClasInfo + "|" + strSkuInfo + "(???)";// +" [" + strJLocInfo + "," + strWLocInfo + "]";
                        return;
                    }
                    else
                    {
                    }
                }
                #endregion
            }
        }

        private void DoLocInfo(IntPtr connID, JToken jtLoc)
        {
            if (jtLoc != null)
            {
                //IP Port解析
                string strIP = "";
                UInt16 uiPort = 0;
                NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

                foreach (DevItemView devItem in lvDevList.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strJLocInfo = "???";
                        string strWLocInfo = "???";

                        JToken jtLat = jtLoc.SelectToken("lat");
                        if (jtLat != null)
                        {
                            strWLocInfo = jtLat.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        JToken jtLng = jtLoc.SelectToken("lng");
                        if (jtLng != null)
                        {
                            strJLocInfo = jtLng.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        devItem.tb_Read_JingDu.Text = strJLocInfo;
                        devItem.tb_Read_WeiDu.Text = strWLocInfo;
                        devItem.SetGprsState();
                        return;
                    }
                }
                foreach (DevItemView devItem in lvDevList1.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strJLocInfo = "???";
                        string strWLocInfo = "???";

                        JToken jtLat = jtLoc.SelectToken("lat");
                        if (jtLat != null)
                        {
                            strWLocInfo = jtLat.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        JToken jtLng = jtLoc.SelectToken("lng");
                        if (jtLng != null)
                        {
                            strJLocInfo = jtLng.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        devItem.tb_Read_JingDu.Text = strJLocInfo;
                        devItem.tb_Read_WeiDu.Text = strWLocInfo;
                        devItem.SetGprsState();
                        return;
                    }
                }
                foreach (DevItemView devItem in lvDevList2.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strJLocInfo = "???";
                        string strWLocInfo = "???";

                        JToken jtLat = jtLoc.SelectToken("lat");
                        if (jtLat != null)
                        {
                            strWLocInfo = jtLat.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        JToken jtLng = jtLoc.SelectToken("lng");
                        if (jtLng != null)
                        {
                            strJLocInfo = jtLng.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        devItem.tb_Read_JingDu.Text = strJLocInfo;
                        devItem.tb_Read_WeiDu.Text = strWLocInfo;
                        devItem.SetGprsState();
                        return;
                    }
                }
                foreach (DevItemView devItem in lvDevList3.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strJLocInfo = "???";
                        string strWLocInfo = "???";

                        JToken jtLat = jtLoc.SelectToken("lat");
                        if (jtLat != null)
                        {
                            strWLocInfo = jtLat.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        JToken jtLng = jtLoc.SelectToken("lng");
                        if (jtLng != null)
                        {
                            strJLocInfo = jtLng.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        devItem.tb_Read_JingDu.Text = strJLocInfo;
                        devItem.tb_Read_WeiDu.Text = strWLocInfo;
                        devItem.SetGprsState();
                        return;
                    }
                }
                foreach (DevItemView devItem in lvDevList4.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strJLocInfo = "???";
                        string strWLocInfo = "???";

                        JToken jtLat = jtLoc.SelectToken("lat");
                        if (jtLat != null)
                        {
                            strWLocInfo = jtLat.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        JToken jtLng = jtLoc.SelectToken("lng");
                        if (jtLng != null)
                        {
                            strJLocInfo = jtLng.ToString();
                            //AddDevInfo(connID, jtProp);
                        }
                        devItem.tb_Read_JingDu.Text = strJLocInfo;
                        devItem.tb_Read_WeiDu.Text = strWLocInfo;
                        devItem.SetGprsState();
                        return;
                    }
                }
            }
        }

        private void DoAPInfo(IntPtr connID, JToken jtAPInfo)
        {
            if (jtAPInfo != null)
            {
                //IP Port解析
                string strIP = "";
                UInt16 uiPort = 0;
                NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

                JToken jtRssi = jtAPInfo.SelectToken("rssi");
                if (jtRssi != null)
                {
                    string strRssiValue = jtRssi.ToString();
                    //strRssiValue = jtAPInfo.Value("rssi").ToString();
                    UpdateDevItemAPInfo(strIP, strRssiValue);
                    int nValue = int.Parse(strRssiValue);

                    bool bNewCreate = false;
                    SignalData data = sdMng.GetSignalData(strIP + "_" + "rssi", true, out bNewCreate);
                    if (data != null)
                    {
                        if (bNewCreate)
                        {
                            //初始化记录表
                            string strLineInfo = strIP;// "Line " + sdMng.SignalDatas.Count.ToString();
                            Color colorLine = FindColorByIp(strIP);//colorList[sdMng.SignalDatas.Count - 1];
                            if (colorLine == Colors.Black)
                            {
                                //扫描时未返回，捕获信号强度时返回了，新增设备
                                //AddDevInfo(recvData.connId, "unkonwn");
                            }
                            else
                            {
                                LineGraph lg = signlRecord_rssi.AddLineGraph(data.mSignalRecord, colorLine, 2, strLineInfo);
                                data.lineGp = lg;

                                DevItemView itemView = FindDevItemViewByIP(strIP);
                                data.strMac = itemView.mac.Text;
                                data.strAliasName = itemView.aliasName.Text;
                                data.strImei = itemView.imei.Text;

                                data.dtStartTime = DateTime.Now;//.ToString();

                                signlRecord_rssi.Viewport.FitToView();
                            }
                        }

                        //Point point = new Point(data.nCount++, nValue);
                        Point point = new Point(dChartTimes, nValue);
                        data.AddNewPoint(base.Dispatcher, point);
                        //mSignalRecord.AppendAsync(base.Dispatcher, point);
                    }
                }
            }
        }

        private void DoASkuInfo(IntPtr connID, JToken jtASku)
        {
            //单独到这里表示可能在OTA升级，判断版本是否变化，若变化表示OTA完成
            if (jtASku != null)
            {
                //IP Port解析
                string strIP = "";
                UInt16 uiPort = 0;
                NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

                string strASku = jtASku.ToString();

                foreach (DevItemView devItem in lvDevList.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strVerClasSkuInfo = devItem.ver.Text.Substring(0, devItem.ver.Text.IndexOf("("));
                        devItem.ver.Text = strVerClasSkuInfo + "(" + strASku + ")";
                        return;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList1.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strVerClasSkuInfo = devItem.ver.Text.Substring(0, devItem.ver.Text.IndexOf("("));
                        devItem.ver.Text = strVerClasSkuInfo + "(" + strASku + ")";
                        return;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList2.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strVerClasSkuInfo = devItem.ver.Text.Substring(0, devItem.ver.Text.IndexOf("("));
                        devItem.ver.Text = strVerClasSkuInfo + "(" + strASku + ")";
                        return;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList3.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strVerClasSkuInfo = devItem.ver.Text.Substring(0, devItem.ver.Text.IndexOf("("));
                        devItem.ver.Text = strVerClasSkuInfo + "(" + strASku + ")";
                        return;
                    }
                    else
                    {
                    }
                }
                foreach (DevItemView devItem in lvDevList4.Children)
                {
                    if (devItem.ip.Text == strIP)
                    {
                        string strVerClasSkuInfo = devItem.ver.Text.Substring(0, devItem.ver.Text.IndexOf("("));
                        devItem.ver.Text = strVerClasSkuInfo + "(" + strASku + ")";
                        return;
                    }
                    else
                    {
                    }
                }
            }
        }

        private void UpdateDevItemAPInfo(string strIP, string strRssiValue)
        {
            foreach (DevItemView devItem in lvDevList.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    devItem.rssi.Text = strRssiValue;
                    break;
                }
                else
                {
                }
            }
            foreach (DevItemView devItem in lvDevList1.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    devItem.rssi.Text = strRssiValue;
                    break;
                }
                else
                {
                }
            }
            foreach (DevItemView devItem in lvDevList2.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    devItem.rssi.Text = strRssiValue;
                    break;
                }
                else
                {
                }
            }
            foreach (DevItemView devItem in lvDevList3.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    devItem.rssi.Text = strRssiValue;
                    break;
                }
                else
                {
                }
            }
            foreach (DevItemView devItem in lvDevList4.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    devItem.rssi.Text = strRssiValue;
                    break;
                }
                else
                {
                }
            }

            //SortDevItemListByRssi(false);
        }

        private void DoNBSignal(IntPtr connID, JToken jtNBSignal)
        {
            if (jtNBSignal != null)
            {
                bool bNewCreate = false;

                //IP Port解析
                string strIP = "";
                UInt16 uiPort = 0;
                NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

                /*
                 * //10:56:41 878  [192.168.3.10 : 59876] 
                BE BE BE BE 
                01 00 00 84 00 00 00 00 12 3A 01 01 
                {
                  "reqId": 40,
                  "err": 0,
                  "prop": {
                    "nbSignal": {
                      "rsrp": "-923",
                      "rsrq": "-112",
                      "ecl": 1,
                      "cellid": 228242770,
                      "pci": 187,
                      "snr": "83",
                      "earfcn": 2507
                    }
                  }
                }
                ED ED 
                 * */

                JToken jtRsrp = jtNBSignal.SelectToken("rsrp");
                if (jtRsrp != null)
                {
                    #region
                    string strValue = jtRsrp.ToString();
                    Int64 nValue = Int64.Parse(strValue);

                    SignalData data = sdMng.GetSignalData(strIP + "_" + "rsrp", true, out bNewCreate);
                    if (data != null)
                    {
                        if (bNewCreate)
                        {
                            //初始化记录表
                            string strLineInfo = strIP;// "Line " + sdMng.SignalDatas.Count.ToString();
                            Color colorLine = FindColorByIp(strIP);//colorList[sdMng.SignalDatas.Count - 1];
                            if (colorLine == Colors.Black)
                            {
                                //扫描时未返回，捕获信号强度时返回了，新增设备
                                //AddDevInfo(recvData.connId, "unkonwn");
                            }
                            else
                            {
                                LineGraph lg = signlRecord_rsrp.AddLineGraph(data.mSignalRecord, colorLine, 2, strLineInfo);
                                data.lineGp = lg;

                                DevItemView itemView = FindDevItemViewByIP(strIP);
                                data.strMac = itemView.mac.Text;
                                data.strAliasName = itemView.aliasName.Text;
                                data.strImei = itemView.imei.Text;

                                data.dtStartTime = DateTime.Now;//.ToString();

                                signlRecord_rsrp.Viewport.FitToView();
                            }
                        }

                        //Point point = new Point(data.nCount++, nValue);
                        Point point = new Point(dChartTimes, nValue);
                        data.AddNewPoint(base.Dispatcher, point);
                        //mSignalRecord.AppendAsync(base.Dispatcher, point);
                    }
                    #endregion
                }

                JToken jtTxPower = jtNBSignal.SelectToken("txPower");
                if (jtTxPower != null)
                {
                    #region
                    string strValue = jtTxPower.ToString();
                    Int64 nValue = Int64.Parse(strValue);

                    SignalData data = sdMng.GetSignalData(strIP + "_" + "txPower", true, out bNewCreate);
                    if (data != null)
                    {
                        if (bNewCreate)
                        {
                            //初始化记录表
                            string strLineInfo = strIP;// "Line " + sdMng.SignalDatas.Count.ToString();
                            Color colorLine = FindColorByIp(strIP);//colorList[sdMng.SignalDatas.Count - 1];
                            if (colorLine == Colors.Black)
                            {
                                //扫描时未返回，捕获信号强度时返回了，新增设备
                                //AddDevInfo(recvData.connId, "unkonwn");
                            }
                            else
                            {
                                LineGraph lg = signlRecord_txpower.AddLineGraph(data.mSignalRecord, colorLine, 2, strLineInfo);
                                data.lineGp = lg;

                                DevItemView itemView = FindDevItemViewByIP(strIP);
                                data.strMac = itemView.mac.Text;
                                data.strAliasName = itemView.aliasName.Text;
                                data.strImei = itemView.imei.Text;

                                data.dtStartTime = DateTime.Now;//.ToString();

                                signlRecord_txpower.Viewport.FitToView();
                            }
                        }

                        //Point point = new Point(data.nCount++, nValue);
                        Point point = new Point(dChartTimes, nValue);
                        data.AddNewPoint(base.Dispatcher, point);
                        //mSignalRecord.AppendAsync(base.Dispatcher, point);
                    }
                    #endregion
                }

                JToken jtSnr = jtNBSignal.SelectToken("snr");
                if (jtSnr != null)
                {
                    #region
                    string strValue = jtSnr.ToString();
                    Int64 nValue = Int64.Parse(strValue);

                    SignalData data = sdMng.GetSignalData(strIP + "_" + "snr", true, out bNewCreate);
                    if (data != null)
                    {
                        if (bNewCreate)
                        {
                            //初始化记录表
                            string strLineInfo = strIP;// "Line " + sdMng.SignalDatas.Count.ToString();
                            Color colorLine = FindColorByIp(strIP);//colorList[sdMng.SignalDatas.Count - 1];
                            if (colorLine == Colors.Black)
                            {
                                //扫描时未返回，捕获信号强度时返回了，新增设备
                                //AddDevInfo(recvData.connId, "unkonwn");
                            }
                            else
                            {
                                LineGraph lg = signlRecord_snr.AddLineGraph(data.mSignalRecord, colorLine, 2, strLineInfo);
                                data.lineGp = lg;

                                DevItemView itemView = FindDevItemViewByIP(strIP);
                                data.strMac = itemView.mac.Text;
                                data.strAliasName = itemView.aliasName.Text;
                                data.strImei = itemView.imei.Text;

                                data.dtStartTime = DateTime.Now;//.ToString();

                                signlRecord_snr.Viewport.FitToView();
                            }
                        }

                        //Point point = new Point(data.nCount++, nValue);
                        Point point = new Point(dChartTimes, nValue);
                        data.AddNewPoint(base.Dispatcher, point);
                        //mSignalRecord.AppendAsync(base.Dispatcher, point);
                    }
                    #endregion
                }

                JToken jtCellID = jtNBSignal.SelectToken("cellid");
                if (jtCellID != null)
                {
                    #region
                    string strValue = jtCellID.ToString();
                    Int64 nValue = Int64.Parse(strValue);

                    SignalData data = sdMng.GetSignalData(strIP + "_" + "cellID", true, out bNewCreate);
                    if (data != null)
                    {
                        if (bNewCreate)
                        {
                            //初始化记录表
                            string strLineInfo = strIP;// "Line " + sdMng.SignalDatas.Count.ToString();
                            Color colorLine = FindColorByIp(strIP);//colorList[sdMng.SignalDatas.Count - 1];
                            if (colorLine == Colors.Black)
                            {
                                //扫描时未返回，捕获信号强度时返回了，新增设备
                                //AddDevInfo(recvData.connId, "unkonwn");
                            }
                            else
                            {
                                LineGraph lg = signlRecord_cellID.AddLineGraph(data.mSignalRecord, colorLine, 2, strLineInfo);
                                data.lineGp = lg;

                                DevItemView itemView = FindDevItemViewByIP(strIP);
                                data.strMac = itemView.mac.Text;
                                data.strAliasName = itemView.aliasName.Text;
                                data.strImei = itemView.imei.Text;

                                data.dtStartTime = DateTime.Now;//.ToString();

                                signlRecord_cellID.Viewport.FitToView();
                            }
                        }

                        //Point point = new Point(data.nCount++, nValue);
                        Point point = new Point(dChartTimes, nValue);
                        data.AddNewPoint(base.Dispatcher, point);
                        //mSignalRecord.AppendAsync(base.Dispatcher, point);
                    }
                    #endregion
                }
            }
        }

        private void DoLogItems(IntPtr connID, JToken jtLogItems)
        {
            if (jtLogItems != null)
            {
                int nCount = DataPackInfoList.Count;
                LogDataItem dataPackItem = new LogDataItem((UInt32)(nCount + 1), jtLogItems);

                //按顺序追加
                //DataPackInfoList.Add(new DataPackItem((UInt32)(nCount + 1), obusPack));
                //逆序插入
                DataPackInfoList.Insert(0, dataPackItem);
            }
        }

        private void DoOTAUDP(IntPtr connID, JToken jObj)
        {
            if (jObj != null)
            {
                JToken jtCmdData = jObj.SelectToken("cmdData");
                if(jtCmdData != null)
                {
                    DoCmdID_CmdData(connID, jtCmdData);
                }
            } 
        }

        private void DoStopOTAUDP(IntPtr connID)
        {
            //IP Port解析
            string strIP = "";
            UInt16 uiPort = 0;
            NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

            foreach (DevItemView devItem in lvDevList.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    //有错误
                    devItem.otaInfo.Text = " OTA失败";
                    devItem.otaInfo.Tag = null;
                    devItem.otaProgress.Visibility = System.Windows.Visibility.Collapsed;
                    devItem.otaInfo.Foreground = new SolidColorBrush(Colors.Red);

                    return;
                }
            }
            foreach (DevItemView devItem in lvDevList1.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    //有错误
                    devItem.otaInfo.Text = " OTA失败";
                    devItem.otaInfo.Tag = null;
                    devItem.otaProgress.Visibility = System.Windows.Visibility.Collapsed;
                    devItem.otaInfo.Foreground = new SolidColorBrush(Colors.Red);

                    return;
                }
            }
            foreach (DevItemView devItem in lvDevList2.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    //有错误
                    devItem.otaInfo.Text = " OTA失败";
                    devItem.otaInfo.Tag = null;
                    devItem.otaProgress.Visibility = System.Windows.Visibility.Collapsed;
                    devItem.otaInfo.Foreground = new SolidColorBrush(Colors.Red);

                    return;
                }
            }
            foreach (DevItemView devItem in lvDevList3.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    //有错误
                    devItem.otaInfo.Text = " OTA失败";
                    devItem.otaInfo.Tag = null;
                    devItem.otaProgress.Visibility = System.Windows.Visibility.Collapsed;
                    devItem.otaInfo.Foreground = new SolidColorBrush(Colors.Red);

                    return;
                }
            }
            foreach (DevItemView devItem in lvDevList4.Children)
            {
                if (devItem.ip.Text == strIP)
                {
                    //有错误
                    devItem.otaInfo.Text = " OTA失败";
                    devItem.otaInfo.Tag = null;
                    devItem.otaProgress.Visibility = System.Windows.Visibility.Collapsed;
                    devItem.otaInfo.Foreground = new SolidColorBrush(Colors.Red);

                    return;
                }
            }
        }

        private void DoCmdID_CmdData(IntPtr connID, JToken jtCmdData)
        {
            if (jtCmdData != null)
            {
                string strErr = "";
                string strStatus = "";
                string strProcess = "";

                JToken jtErr= jtCmdData.SelectToken("err");
                if (jtErr != null)
                {
                    strErr = jtErr.ToString();
                }

                JToken jtStatus = jtCmdData.SelectToken("status");
                if (jtStatus != null)
                {
                    strStatus = jtStatus.ToString();
                }

                JToken jtProcess = jtCmdData.SelectToken("process");
                if (jtProcess != null)
                {
                    strProcess = jtProcess.ToString();
                }

                //IP Port解析
                string strIP = "";
                UInt16 uiPort = 0;
                NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

                bool bOK = DoUpdateOTAInfo(lvDevList, strErr, strStatus, strProcess, strIP, connID);
                if(bOK == false)
                {
                    bOK = DoUpdateOTAInfo(lvDevList1, strErr, strStatus, strProcess, strIP, connID);
                    if (bOK == false)
                    {
                        bOK = DoUpdateOTAInfo(lvDevList2, strErr, strStatus, strProcess, strIP, connID);
                        if (bOK == false)
                        {
                            bOK = DoUpdateOTAInfo(lvDevList3, strErr, strStatus, strProcess, strIP, connID);
                            if (bOK == false)
                            {
                                bOK = DoUpdateOTAInfo(lvDevList4, strErr, strStatus, strProcess, strIP, connID);
                            }
                        }
                    }
                }
            } 
        }

        private bool AnalysisJToken(IntPtr connID, JToken jtProp)
        {
            JToken jtWifiInfo = jtProp.SelectToken("wifiInfo");
            JToken jtNBBaseInfo = jtProp.SelectToken("nbBase");
            JToken jtFWV = jtProp.SelectToken("fwv");
            JToken jtLoc = jtProp.SelectToken("loc");

            JToken jtClas = jtProp.SelectToken("clas");
            JToken jtSku = jtProp.SelectToken("sku");
            JToken jtASku = jtProp.SelectToken("asku");

            if (jtWifiInfo != null && jtNBBaseInfo != null /*&& jtFWV != null*/)
            {
                if(jtClas != null && jtSku != null)
                {                    
                    return AddDevInfo_Ex(connID, jtWifiInfo, jtNBBaseInfo, jtFWV, jtLoc, jtClas, jtSku, jtASku);
                }
                else
                {
                    return AddDevInfo(connID, jtWifiInfo, jtNBBaseInfo, jtFWV, jtLoc);
                }                
            }                
            else
            {
                if (jtFWV != null)
                {
                    if (jtClas != null && jtSku != null)
                    {
                        DoFWV_Clas_SkuInfo(connID, jtFWV, jtClas, jtSku);
                    }
                    else
                    {
                        DoFWVInfo(connID, jtFWV);
                    }
                }                    

                if (jtLoc != null)
                    DoLocInfo(connID, jtLoc);

                if (jtASku != null)
                    DoASkuInfo(connID, jtASku);
            }
            

            JToken jtAPInfo = jtProp.SelectToken("apInfo");
            if (jtAPInfo != null)
                DoAPInfo(connID, jtAPInfo);

            JToken jtNBSignal = jtProp.SelectToken("nbSignal");
            if (jtNBSignal != null)
                DoNBSignal(connID, jtNBSignal);

            //JToken jtLogItems = jtProp.SelectToken("items");
            //if (jtLogItems != null)
            //    DoLogItems(connID, jtLogItems);

            return true;
        }        
    }
}
